package zijie;

import java.util.HashSet;
import java.util.Set;

public class Main {
    //双指针的思想解决
    public String addStrings(String num1, String num2) {
        int len1 = num1.length() - 1;
        int len2 = num2.length() - 1;
        int carry = 0;
        StringBuffer sb = new StringBuffer();
        while (len1 >= 0 || len2 >= 0) {
            int x = len1 >= 0 ? num1.charAt(len1) - '0' : 0;
            int y = len2 >= 0 ? num2.charAt(len2) - '0' : 0;
            int cur = x + y + carry; //当前的
            carry = cur / 10;
            len1--;
            len2--;
            sb.append(cur % 10);
        }
        if (carry != 0) {
            sb.append(carry);
        }
        return sb.reverse().toString();
    }
    //大数相乘
    public String multiply(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        int  len1=num1.length();
        int  len2=num2.length();
        int  m=len2+len1;
        int[] arr =new int[len1+len2];
        for (int i = num1.length()-1; i >=0 ; i--) {
            int x=num1.charAt(i) - '0';
            for (int j = num2.length()-1; j >=0 ; j--) {
                int y=num2.charAt(j) - '0';
              arr[i+j+1]=arr[i+j+1]+x*y;
            }
        }
        for (int i = m-1; i>0; i--) {
            arr[i-1]+=arr[i]/10;
            arr[i]=arr[i]%10;  //刚开始这个漏掉了  a[i]需要和10进行取模
        }
        StringBuffer sb = new StringBuffer();
        int index=arr[0]==0?1:0;
        for (int i=index; i<arr.length; i++) {
            sb.append(arr[i]);
        }
        return sb.toString();
    }

    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int n = s.length();
        if (n == 0){
            return 0;
        }
        int maxLen = 0;
        int i = 0, j = 0;
        while (j <n) {
            char c= s.charAt(j);
            if (set.contains(c)) {
                //如果包含了c，则i指针向右移动，直到不再包含c
//                while (set.contains(c)) {
//                    set.remove(s.charAt(i));
//                    i++;
//                }
                //对于相等的并没有进行
                while (s.charAt(i) != c) {
                    set.remove(s.charAt(i));
                    i++;
                }
                //需要跳过当前的字符
                i++;
            }else{
                set.add(c);
                maxLen = Math.max(maxLen, j - i + 1);
            }
            j++;
        }
        return maxLen;
    }
}
